﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using SalesSys.BLL;
using SalesSys.DAL.Models;
using SalesSys.WebCore;
using SalesSys.WebCore.Authorize;

namespace SalesSys.Site.Controllers.Api
{
    [RoutePrefix("api/Approval")]
    public class ApprovalController : BaseApiController
    {
        private SequenceApprovalBLL bll = new SequenceApprovalBLL();

        [Route("List")]
        public ViewDtos<SequenceApproval> GetList(string title = null, ApprovalState? state = null, int page = 1,
            int rows = 10)
        {
            var re = bll.GetData(title, state, ClientHelper.ClientInfo.Id, page, rows);
            // var re = bll.GetPaging(p => p.Title.Contains(title) && p.NowAppoverId == ClientHelper.ClientInfo.Id, p => p.CreateTime, true, page, rows);
            var dto = ViewDto.CreateList(re.Value);
            dto.Page = page;
            dto.Rows = rows;
            dto.Total = re.Key;
            return dto;
        }

        [HttpGet]
        [Route("AdminGet")]
        public ViewDtos<SequenceApproval> GetAdminGet(string title = null, ApprovalState? state = null,
            bool isAll = false, int page = 1, int rows = 10)
        {

            var re = bll.AdminGetData(title, state, isAll, page, rows);
            // var re = bll.GetPaging(p => p.Title.Contains(title) && p.NowAppoverId == ClientHelper.ClientInfo.Id, p => p.CreateTime, true, page, rows);
            var dto = ViewDto.CreateList(re.Value);
            dto.Page = page;
            dto.Rows = rows;
            dto.Total = re.Key;
            return dto;
        }

        public SequenceApproval Get(Guid id)
        {
            return bll.Details(id);
        }

        [Route("Approval")]
        public void Approval(Guid id, bool pass, string opinion)
        {
            try
            {
                var handler = new ApprovalHandlers(id);
                var emp = new EmployeeBLL().Details(ClientHelper.ClientInfo.Id);
                handler.DoApproval(pass, opinion, emp);
            }
            catch (Exception ex)
            {
                if (!string.IsNullOrWhiteSpace(ex.Message))
                    throw new ArgumentException(ex.Message);
                else
                {
                    throw;
                }
            }

        }

        [Route("Manage")]
        public string[] GetManage()
        {
            return ContextApprovalBase.GetApprovalWorks().Select(p => p.TypeName).ToArray();
        }


        [Route("ManageItems")]
        public IEnumerable<SequenceApprovalItem> GetManageItems(string typeName)
        {
            if (typeName == null)
            {
                throw new ApiArgException();
            }
            var appw = ContextApprovalBase.GetApprovalWorks().FirstOrDefault(p => p.TypeName == typeName);
            BaseBLL<SequenceApprovalItem, Guid> itemBll = new BaseBLL<SequenceApprovalItem, Guid>();
            return itemBll.GetList(p => p.TypeName == typeName);
            if (appw != null) return appw.Items;
            return null;
        }

        //public void Put(SequenceApprovalItem item)
        //{
        //     bll.EditItem(item);
        //}

        //public void Post(SequenceApprovalItem item)
        //{
        //    bll.AddItem(item);
        //}

        //public void Delete(Guid itemId)
        //{
        //    bll.DeleteItem(itemId);
        //}
    }
}
